package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.sys.*;
import com.example.ymm.modules1.query.SysMenuQueryCriteria;
import com.example.ymm.modules1.query.sys.SysApprovalSettingLineQueryCriteria;
import com.example.ymm.modules1.query.sys.SysApprovalSettingNodeQueryCriteria;
import com.example.ymm.modules1.query.sys.SysApprovalSettingQueryCriteria;
import com.example.ymm.modules1.query.sys.SysRoleQueryCriteria;
import com.example.ymm.modules1.service.sys.*;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/approval")
@Api(tags = "pc 审批设置管理")
@Slf4j
@CrossOrigin
public class SysApprovalController extends BaseController {

    @Autowired
    SysApprovalSettingService sysApprovalSettingService;
    @Autowired
    SysApprovalSettingLineService sysApprovalSettingLineService;

    @Autowired
    SysApprovalSettingNodeService sysApprovalSettingNodeService;

    @ApiOperation(value = "审批设置列表")
    @RequestMapping(value = "/approvalSettingList", method = RequestMethod.POST)
    public CommonResult approvalSettingList(@Valid SysApprovalSettingQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<SysApprovalSetting> list = sysApprovalSettingService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "审批设置保存")
    @RequestMapping(value = "/saveApprovalSetting", method = RequestMethod.POST)
    public CommonResult saveApprovalSetting(SysApprovalSetting approvalSetting) {
        try{
            sysApprovalSettingService.saveOrUpdate(approvalSetting);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "审批设置删除")
    @RequestMapping(value = "/approvalSettingDelete", method = RequestMethod.POST)
    public CommonResult approvalSettingDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String approvalSettingId) {
        try{
            sysApprovalSettingService.removeById(approvalSettingId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }





    @ApiOperation(value = "审批链路列表")
    @RequestMapping(value = "/settingLineList", method = RequestMethod.POST)
    public CommonResult settingLineList(@Valid SysApprovalSettingLineQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<SysApprovalSettingLine> list = sysApprovalSettingLineService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "审批链路保存")
    @RequestMapping(value = "/saveSettingLine", method = RequestMethod.POST)
    public CommonResult saveSettingLine(@RequestBody SysApprovalSettingLine settingLine) {
        try{
            if(UtilValidate.isEmpty(settingLine.getLineType())){
                return CommonResult.error("数据不能为空!");
            }
            if(UtilValidate.isEmpty(settingLine.getApprovalSettingId())){
                return CommonResult.error("设置id不能为空!");
            }
            sysApprovalSettingLineService.saveSettingLine(settingLine);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "审批链路删除")
    @RequestMapping(value = "/settingLineDelete", method = RequestMethod.POST)
    public CommonResult settingLineDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String approvalSettingLineId) {
        try{
            sysApprovalSettingLineService.removeById(approvalSettingLineId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "审批节点列表")
    @RequestMapping(value = "/settingNodeList", method = RequestMethod.POST)
    public CommonResult settingNodeList(@Valid SysApprovalSettingNodeQueryCriteria criteria) {
        try{
            List<SysApprovalSettingNode> list = sysApprovalSettingNodeService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "审批节点保存")
    @RequestMapping(value = "/saveSettingNode", method = RequestMethod.POST)
    public CommonResult saveSettingNode(SysApprovalSettingNode settingNode) {
        try{
            sysApprovalSettingNodeService.saveOrUpdate(settingNode);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "审批节点删除")
    @RequestMapping(value = "/settingNodeDelete", method = RequestMethod.POST)
    public CommonResult settingNodeDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String approvalSettingNodeId) {
        try{
            sysApprovalSettingNodeService.removeById(approvalSettingNodeId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


}
